# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=ncurses
pkgver=6.4_p20240420
_pkgver=${pkgver/_p/-}
pkgrel=0
pkgdesc="Console display library"
url="https://invisible-island.net/ncurses/"
arch="all"
options="!check" # "tests" are actual demo programs, not a test suite.
license="X11"
makedepends_build="ncurses"
subpackages="
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-terminfo-base:base:noarch
	$pkgname-terminfo:terminfo:noarch
	$pkgname-libs:compat:noarch
	libformw
	libmenuw
	libncurses++:pp
	libncursesw
	libpanelw
	"
source="https://invisible-mirror.net/archives/ncurses/current/ncurses-$_pkgver.tgz
	cleanup-pkgconfig-ldflags.patch
	"
builddir="$srcdir/$pkgname-$_pkgver"

# secfixes:
#   6.4_p20230424-r0:
#     - CVE-2023-29491
#   6.3_p20220416-r0:
#     - CVE-2022-29458
#   6.2_p20200530-r0:
#     - CVE-2021-39537
#   6.1_p20180414-r0:
#     - CVE-2018-10754
#   6.0_p20171125-r0:
#     - CVE-2017-16879
#   6.0_p20170701-r0:
#     - CVE-2017-10684

# Terminfo definitions to be included in ncurses-terminfo-base.
_basic_terms="
	alacritty
	ansi
	console
	dumb
	gnome
	gnome-256color
	konsole
	konsole-256color
	konsole-linux
	linux
	putty
	putty-256color
	rxvt
	rxvt-256color
	screen
	screen-256color
	st-*
	sun
	terminator
	terminology*
	tmux
	tmux-256color
	vt100
	vt102
	vt200
	vt220
	vt52
	vte
	vte-256color
	xterm
	xterm-256color
	xterm-color
	xterm-xfree86
	"

build() {
	export CFLAGS="$CFLAGS -D_GNU_SOURCE"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--mandir=/usr/share/man \
		--without-ada \
		--without-tests \
		--disable-termcap \
		--disable-root-access \
		--disable-rpath-hack \
		--disable-setuid-environ \
		--disable-stripping \
		--with-pkg-config-libdir=/usr/lib/pkgconfig \
		--with-cxx-binding \
		--with-cxx-shared \
		--with-terminfo-dirs="/etc/terminfo:/usr/share/terminfo:/lib/terminfo:/usr/lib/terminfo" \
		--enable-pc-files \
		--with-shared \
		--enable-widec
	make
}

package() {
	make -j1 DESTDIR="$pkgdir" install

	cd "$pkgdir"

	# force link against *w.so
	local lib; for lib in ncurses ncurses++ form panel menu; do
		ln -s ${lib}w.pc usr/lib/pkgconfig/$lib.pc
		ln -s lib${lib}w.a usr/lib/lib$lib.a
		echo "INPUT(-l${lib}w)" > usr/lib/lib$lib.so
	done

	# link curses -> ncurses
	ln -s libncurses.a usr/lib/libcurses.a
	ln -s libncurses.so usr/lib/libcurses.so
	echo 'INPUT(-lncursesw)' > usr/lib/libcursesw.so

	# Install basic terms in /etc/terminfo
	local i; for i in $_basic_terms; do
		local termfiles=$(find usr/share/terminfo/ -name "$i" 2>/dev/null) || true

		[ -z "$termfiles" ] && continue

		for termfile in $termfiles; do
			local basedir=$(basename "$(dirname "$termfile")")
			install -d etc/terminfo/$basedir
			mv "$termfile" etc/terminfo/$basedir/
			ln -s "../../../../etc/terminfo/$basedir/${termfile##*/}" \
				"usr/share/terminfo/$basedir/${termfile##*/}"
		done
	done
}

dev() {
	default_dev

	# also move symlinks
	amove usr/lib/*.so
}

terminfo() {
	pkgdesc="$pkgdesc (other terminfo files)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"
	replaces="kitty<0.25.2-r1"  # /usr/share/terminfo/x/xterm-kitty (Alpine <3.17)

	amove usr/share/terminfo
	# also move symlink
	amove usr/lib/terminfo
}

compat() {
	pkgdesc="Ncurses libraries (old compat to pull sublibs)"
	provides="ncurses-widec-libs=$pkgver-r$pkgrel"
	depends="
		libformw=$pkgver-r$pkgrel
		libmenuw=$pkgver-r$pkgrel
		libncursesw=$pkgver-r$pkgrel
		libpanelw=$pkgver-r$pkgrel
		"

	mkdir -p "$subpkgdir"
}

libmenuw() {
	pkgdesc="$pkgdesc ($subpkgname)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"

	amove usr/lib/libmenuw.so.*
}

libformw() {
	pkgdesc="$pkgdesc ($subpkgname)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"

	amove usr/lib/libformw.so.*
}

pp() {
	pkgdesc="$pkgdesc ($subpkgname)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"

	amove usr/lib/libncurses++w.so.*
}

libncursesw() {
	pkgdesc="$pkgdesc ($subpkgname)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"

	amove usr/lib/libncursesw.so.*
}

libpanelw() {
	pkgdesc="$pkgdesc ($subpkgname)"
	depends="$pkgname-terminfo-base=$pkgver-r$pkgrel"

	amove usr/lib/libpanelw.so.*
}

base() {
	pkgdesc="Descriptions of common terminals"
	depends=""

	amove etc/terminfo
}

sha512sums="
1cd453045778fc6eaaff867bc2b1fbec5be61f4a9e65eb6ac90bffcdc712f40f5d7726c488d413119c6b547be528d75a16bd5cda66065e128273ca909ebd15da  ncurses-6.4-20240420.tgz
201ef1876655101cedabc83a0ce46f75079b08f565ca8de4cf96fd69e41332a2d0597b77fe360dc58b10772586fa39bd52ac9ee670a912fef84840278356065a  cleanup-pkgconfig-ldflags.patch
"
